package com.umeox.watch.moto.dataservice.db.model;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

import com.umeox.moto.common.utils.DateUtils;
import com.umeox.moto.common.utils.GsonUtils;
import com.umeox.watch.location.proto.LocationProto;

import java.util.ArrayList;
import java.util.List;

@Entity(tableName = LocationInfo.TABLE_NAME)
public class LocationInfo {
    @Ignore
    public static final String TABLE_NAME = "location_record";

    @NonNull
    @PrimaryKey(autoGenerate = true)
    private long id;

    private String eventId;

    private String holderId;

    private double lat = -999d;

    private double lng = -999d;

    private int accuracy = -1;
    /**
     * 定位开始时间 格式：yyyy-MM-ddTHH:mm:ssZ  例：20200520T000000+0800
     */

    private String startTime;
    /**
     * 定位结束时间 格式：yyyy-MM-ddTHH:mm:ssZ  例：20200520T000000+0800
     */

    private String endTime;

    private String address;
    /**
     * 0：来自于服务端命令的上报
     * 1：正常基础信息上报
     * 2：通话上报位置
     * 3：关机上报位置
     * 4： SOS触发上报位置
     */
    private int reportReason;
    /**
     * 海外使用Google SDK定位，包括GPS和网络定位两种模式，定义网络定位为WIFI定位
     * 0：GPS
     * 2：WIFI
     */

    private int locationMode;

    private int order;//定位顺序

    private double speed;

    private double direction = -1;

    private double height;

    private int temperature;

    private int battery = -1;

    private String cellTower;

    private String nearbyCellTowers;

    private String wifiPoints;

    private String network;

    private int charge;//是否在充电 0 否 1 是
    /**
     * 上传到服务端标记
     */
    private int reportFlag;

    public static LocationInfo mergeFromProto(LocationProto.LocationDetail info) throws Exception {
        if (info != null) {
            final LocationInfo locationInfo = new LocationInfo();
            locationInfo.setEventId(info.getEventId());
            locationInfo.setHolderId(info.getHolderId());
            locationInfo.setLat(info.getLat());
            locationInfo.setLng(info.getLng());
            locationInfo.setAccuracy((int) info.getAccuracy());
            locationInfo.setStartTime(DateUtils.formatDateTime(info.getStartTime(),"yyyyMMdd'T'HHmmssZ"));
            locationInfo.setEndTime(DateUtils.formatDateTime(info.getEndTime(),"yyyyMMdd'T'HHmmssZ"));
            locationInfo.setAddress(info.getAddress());
            locationInfo.setLocationMode(info.getLocModeValue());
            locationInfo.setReportReason(info.getLocReasonValue());
            locationInfo.setLocationMode(info.getLocModeValue());
            locationInfo.setOrder(info.getOrder());
            locationInfo.setSpeed(info.getSpeed());
            locationInfo.setDirection(info.getDirection());
            locationInfo.setHeight(info.getHeight());
            locationInfo.setTemperature(info.getTemperature());
            locationInfo.setBattery(info.getBattery());
            locationInfo.setCellTower(info.getCellTower());
            locationInfo.setNearbyCellTowers(toNearbyCellTowers(info.getNearbyCellTowersList()));
            locationInfo.setWifiPoints(toWifiPoints(info.getWifiPointsList()));
            locationInfo.setNetwork(info.getNetwork());
            locationInfo.setCharge(info.getCharge());
            return locationInfo;
        }
        return null;
    }

    public static String toNearbyCellTowers(List<String> cellTowerList) {

        return cellTowerList == null ? "[]" : GsonUtils.toJson(cellTowerList);
    }

    public static String toWifiPoints(List<LocationProto.WifiPoint> wifiPointList) throws Exception {
        if (wifiPointList != null) {
            List<String[]> wifiPoints = new ArrayList<>(wifiPointList.size());
            for (LocationProto.WifiPoint wifiPoint : wifiPointList) {
                List<String> tempList = wifiPoint.getWifiPointList();
                wifiPoints.add(tempList.toArray(new String[0]));
            }
            return GsonUtils.objectToArray(wifiPoints).toString();
        }
        return null;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getEventId() {
        return eventId;
    }

    public void setEventId(String eventId) {
        this.eventId = eventId;
    }

    public String getHolderId() {
        return holderId;
    }

    public void setHolderId(String holderId) {
        this.holderId = holderId;
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLng() {
        return lng;
    }

    public void setLng(double lng) {
        this.lng = lng;
    }

    public int getAccuracy() {
        return accuracy;
    }

    public void setAccuracy(int accuracy) {
        this.accuracy = accuracy;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getReportReason() {
        return reportReason;
    }

    public void setReportReason(int reportReason) {
        this.reportReason = reportReason;
    }

    public int getLocationMode() {
        return locationMode;
    }

    public void setLocationMode(int locationMode) {
        this.locationMode = locationMode;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public double getDirection() {
        return direction;
    }

    public void setDirection(double direction) {
        this.direction = direction;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public int getTemperature() {
        return temperature;
    }

    public void setTemperature(int temperature) {
        this.temperature = temperature;
    }

    public int getBattery() {
        return battery;
    }

    public void setBattery(int battery) {
        this.battery = battery;
    }

    public String getCellTower() {
        return cellTower;
    }

    public void setCellTower(String cellTower) {
        this.cellTower = cellTower;
    }

    public String getNearbyCellTowers() {
        return nearbyCellTowers;
    }

    public void setNearbyCellTowers(String nearbyCellTowers) {
        this.nearbyCellTowers = nearbyCellTowers;
    }

    public String getWifiPoints() {
        return wifiPoints;
    }

    public void setWifiPoints(String wifiPoints) {
        this.wifiPoints = wifiPoints;
    }

    public String getNetwork() {
        return network;
    }

    public void setNetwork(String network) {
        this.network = network;
    }

    public int getCharge() {
        return charge;
    }

    public void setCharge(int charge) {
        this.charge = charge;
    }

    public int getReportFlag() {
        return reportFlag;
    }

    public void setReportFlag(int reportFlag) {
        this.reportFlag = reportFlag;
    }

    @Override
    public String toString() {
        return "LocationInfo{" +
                "id=" + id +
                ", eventId='" + eventId + '\'' +
                ", holderId='" + holderId + '\'' +
                ", lat=" + lat +
                ", lng=" + lng +
                ", accuracy=" + accuracy +
                ", startTime='" + startTime + '\'' +
                ", endTime='" + endTime + '\'' +
                ", address='" + address + '\'' +
                ", reportReason=" + reportReason +
                ", locationMode=" + locationMode +
                ", order=" + order +
                ", speed=" + speed +
                ", direction=" + direction +
                ", height=" + height +
                ", temperature=" + temperature +
                ", battery=" + battery +
                ", cellTower='" + cellTower + '\'' +
                ", nearbyCellTowers='" + nearbyCellTowers + '\'' +
                ", wifiPoints='" + wifiPoints + '\'' +
                ", network='" + network + '\'' +
                ", charge=" + charge +
                ", reportFlag=" + reportFlag +
                '}';
    }
}
